import redis
import re
import random
import time
import random
import os
import json

import requests

import threading
import queue
import time


def crawlSummaryPart(projectId=6, time=420):
    """"
    爬取警报数据预览,包含工程监测的项目以及预警阈值
    """
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'cookie': cookie,
        'Connection': 'keep-alive'
    }
    url = baseUrl+"/DataReport/GetProjectSummaryByTime"
    params = {
        "pid": projectId,
        "time": time
    }
    requestResult = requests.get(url=url, params=params, headers=headers)
    rawContent = requestResult.text
    key = "summary_"+str(time)+"_proj"+str(projectId)
    redisDataBase.set(key, rawContent)


def globalVariance():
    """
    定义全局变量
    """
    global cookie
    print("输入从浏览器获得的cookie")
    # cookie=input()
    cookie = "ss_TongJi-projectId=6; _shensui=53D1C4222CEB1C51EB2AABBD97DC5CE25F5D4D341FAECF31C6880BC3926F5A9AFE369319FEE96DAFD381C121A8A3C9E14DB2832461E4B36A884DC0921B012F5B38A334AE59FA30D448C72774A0D400CBBC08891BDBA6D344588060C1EF490E7772C8166D7A50EFB2A45713FB815A11D2423298D7AF634EFC41FD9AF80A0794EFFF39E16E92F0C3E6F11B5F3194F97102"

    global redisDataBase
    redisDataBase = redis.StrictRedis(
        host='localhost', port=6379, db=0)  # 内存数据库，用于数据缓存，方便后续一次性存储
    global baseUrl
    baseUrl = "http://47.97.46.65:8833/Monitoring/"
    global MonitTypeDefinition
    MonitTypeDefinition = {
        "107": "StrssOfWallBar",  # 内墙钢筋应力
        "108": "StrainOfWall",
        "44": "ZP01",  # 云岭深层水平位移
        "38": "ZP02",
        "48": "ZP03",
        "46": "ZP04",
        "72": "ZP05",
        "40": "ZP06",
        "96": "ZZP02",
        "97": "ZZP03",
        "64": "ST1",  # 云岭维护墙侧向水压力
        "66": "ST2",
        "65": "ST3",
        "67": "ST4",
        "68": "TY1",
        "70": "TY2",
        "69": "TY3",
        "71": "TY4",
        "51": "QL1H",
        "52": "QL1V",
        "54": "QL3H",
        "55": "QL3V",
        "56": "QL4H",
        "57": "QL4V",
        "58": "QL5H",
        "59": "QL5V",
        "60": "QL6H",
        "61": "QL6V",
        "62": "QL2H",
        "63": "QL2V"
    }


def startCrawl():
    pass


def main():
    globalVariance()
    # 手动监测
    # http://47.97.46.65:8833/WorkSystem/GetProjectList
    # id 4:云岭
    # ID 6:苗圃
    projList = [4, 6]
    print("按回车开始爬取最新数据")
    x = input()


def main4AutoMonitor():
    """
    用于自动化监测数据爬取
    """
    globalVariance()
    # http://47.97.46.65:8833/Monitoring/GetAutoSubProjects
    # id 12:云岭
    # ID 11:苗圃
    projList = [4, 6]
    print("开始爬取最新数据")


def test():
    """
    测试用主程序
    """
    globalVariance()


test()
